/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanosn.coffeemaker;

import com.nanosn.coffeemaker.ant.Ant;
import com.nanosn.coffeemaker.databaseproject.ProjectGenerator;
import com.nanosn.coffeemaker.freemarker.FreeMarker;
import com.nanosn.linux.Linux;
import com.nanosn.utils.LocateFiles;
import freemarker.template.TemplateException;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Logger;

/**
 * this class holds the constant global variables and the main
 * application functions.
 * @author selkhateeb
 */
public class Application
{
    // <editor-fold defaultstate="collapsed" desc="Application Settings">
    // <editor-fold defaultstate="collapsed" desc="     Public Static Fields">

    /**
     * Gets the directory where the generated files are stored
     */
    public static String GENERATED_FILES_DIRECTORY = "generated";
    /**
     * Gets the package directory in which the template files are located.
     */
    public static String TEMPLATE_FILES_PACKAGE_DIRECTORY =
            "com/nanosn/coffeemaker/templates";
    /**
     * Gets the current directory
     */
    public static String PWD;
    static
    {
        // For performance and optimization
        PWD = System.getProperty("user.dir") + File.separator;
    }

    public static String NAME = "CoffeeMaker";
    
    public static String TMP;
    static
    {
         // For performance and optimization
        TMP = System.getProperty("java.io.tmpdir") +
                               File.separator + Application.NAME +
                               File.separator;
    }
    /**
     * Gets the global logger to be used across the application
     */
    public static Logger logger = Logger.global;
    
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="     Public Static Functions">
    /**
     * Pre: The application jar file exists.
     * Post: The full path of the application jar is returned as a File
     * @return a File object pointing at the application jar.
     * @throws java.io.IOException
     */
    public static File getApplicationJar() throws IOException
    {
        return LocateFiles.getClassLocation(new Application().getClass());
    }

    /**
     * Gets the version information form the manifest file.
     * User passed Command-Line Argument: (-v --version)
     * Pre:
     * Post: The version information has returned
     * @return the version information as String
     * @throws java.io.IOException
     */
    public static String getVersion() throws IOException
    {

        Application app = new Application();
        Package pkg = app.getClass().getPackage();

        String version = pkg.getSpecificationTitle() + " - " +
                         pkg.getImplementationTitle() + " (" +
                         pkg.getSpecificationVersion() +
                         pkg.getImplementationVersion() + ")";

        return version;

    }
    // </editor-fold>
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Start Project">
    /**
     * Pre: User is creating a new project.
     * Post: the initial directory structure of the project is created.
     * @param projectName the name of the project that the user wants to create.
     * @throws IOException
     * @throws TemplateException 
     */
    public static void StartProject(String projectName)
            throws IOException, TemplateException
    {
        String settingsTemplateFileName = "Settings.ftl";
        String settingsTemplateFile = TEMPLATE_FILES_PACKAGE_DIRECTORY +
                                      File.separator + "application" +
                                      File.separator + settingsTemplateFileName;


        //Create Project directory
        File projectDirectory =
                new File(Application.PWD + File.separator + projectName);
        projectDirectory.mkdirs();
        
        FreeMarker.ProcessTemplateFileLocatedInJarFile(
                getApplicationJar(),
                settingsTemplateFile,
                projectName + File.separator + "settings.properties",
                new HashMap(0));
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Start Application">
    /**
     * Creates the application directory
     * Pre: applicaitonName has been initialized.
     *      - User passed Command-Line Argument: (--startapp)
     * Post:
     * @param applicationName
     */
    public static void StartApplicaiton(String applicationName)
    {
        File startupDirectories = new File(applicationName +
                                           File.separator +
                                           "models");
        startupDirectories.mkdirs();
    }

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Print Sql Schema">
    /**
     * Prints the SQL schema of the defined classes.
     * Pre: User has selected a name for the application
     * Post: The SQL schema of the defined entity classed has been displayed to
     *       output stream
     * @param applicationName The application name
     * @throws Exception 
     */
    public static void PrintSqlSchema(String applicationName) throws Exception
    {
        System.out.println("This should print table structure for " + applicationName);

        ProjectGenerator.GenerateProject(applicationName);
        
        Ant.Build(GENERATED_FILES_DIRECTORY + File.separator +
                applicationName + File.separator + "build.xml");
        Ant.Run(GENERATED_FILES_DIRECTORY + File.separator +
                applicationName + File.separator + "build.xml");
        Linux.cat(GENERATED_FILES_DIRECTORY + File.separator +
                applicationName + File.separator + "sql/create-ddl.sql");

    }
    // </editor-fold>
}
